// pages/order/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabTitle: [
      { id: 0, name: '正在进行中', isSelected: true },
      { id: 1, name: '历史订单', isSelected: false }
    ],
    ordersList: []
  },


  /**
   * 正在进行中订单
   */
  handOrder: function () {
    const that = this
    var ordersList = that.data.ordersList
    wx.request({
     
      url: app.globalData.website + 'Orderform/getOrderList',
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        console.log(res)
        ordersList = res.data.data
        that.setData({
          ordersList: ordersList
        })
      }
    })
  },

  /**
   * 历史订单
   */
  finishOrders: function () {
    const that = this
    var ordersList = that.data.ordersList
    wx.request({

      url: app.globalData.website + 'Orderform/getOrderList',
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        console.log(res)
        ordersList = res.data.data
        that.setData({
          ordersList: ordersList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    that.handOrder()
  },

  /**
   * 点击tab选项
   */
  tabClick: function (e) {
    var that = this;
    const currentIndex = e.currentTarget.dataset.index
    const value = "tabTitle[" + currentIndex + "].isSelected"
    const tabTitle = this.data.tabTitle
    for (var i = 0; i < tabTitle.length; i++) {
      if (tabTitle[i].id === currentIndex) {
        tabTitle[i].isSelected = true
        if (tabTitle[i].id === 0) {
          that.handOrder()
        } else {
          that.finishOrders()
        }
      } else {
        tabTitle[i].isSelected = false
      }
    }
    that.setData({
      tabTitle: tabTitle
    })
  },

  /**
   * 点击订单进入详情页
   */
  // showDetails: function () {
  //   wx.navigateTo({
  //     url: '../order-details/index',
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})